/*
 * @Descripttion: 享元模式
 * @Author: lcx
 * @version: v01
 * @Date: 2023-10-17 23:02:55
 * @LastEditors: lcx
 * @LastEditTime: 2023-10-17 23:19:59
 */
abstract class Piece {
    String color;

    abstract void location(int x, int y);
}

class PieceFactory {
    Piece[] pieces = { new WhitePiece(), new BlackPiece() };

    public Piece getPiece(boolean index) {
        if (index)
            return pieces[0];
        return pieces[1];
    }

}

class WhitePiece extends Piece {
    void location(int x, int y) {
        System.out.println("x: " + x + " y: " + y);
    }
}

class BlackPiece extends Piece{
    void location(int x,int y)
    {
        System.out.println("x: " + x + " y: " + y);
    }
}

class test {
    public static void main(String[] args) {
        PieceFactory pieceFactory = new PieceFactory();
        Piece piece1 = pieceFactory.getPiece(false);
        piece1.location(20,22);
        System.out.println(piece1);
        
        Piece piece2 = pieceFactory.getPiece(false);
        piece2.location(20, 23);
        System.out.println(piece2);
    }


}